package com.zb.manager.controller;

import com.zb.model.TGoods;
import com.zb.service.ManagerService;
import com.zb.service.impl.CategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**  

* <p>Title: GoodsController</p>  

* <p>Description: ������Ʒ</p>  

*/
@Controller
@RequestMapping("/goods")
public class GoodsControllers {
	
	@Autowired
	private ManagerService ms;
	
	@Autowired
	private CategoryServiceImpl cs;
	
	@GetMapping("/goods_list")
	public String goods_list(HttpSession session) throws Exception{
		List<TGoods> goodsList= ms.selectAllGoodsInDB();
		session.setAttribute("goodsList", goodsList);
		return "admin/goodslist";
	}
	
	@GetMapping("/goods_stock_list")
	public String goods_stock_list(HttpSession session) throws Exception{
		List<TGoods> goodsList= ms.selectAllGoodsInDB();
		session.setAttribute("goodsList", goodsList);
		return "admin/stocklist";
	}
	
	@RequestMapping("/addGoods")
	public String addGoods(HttpServletRequest request,HttpServletResponse response)throws Exception{
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
		//,@RequestParam(value="id",required=false)MultipartFile goodsimg
		String uuida = UUID.randomUUID().toString();		
		//String type=FilenameUtils.getExtension(goodsimg.getOriginalFilename());
		//System.out.println(""+type);
		//goodsimg.transferTo(new File("D:\\log\\" +uuida  + "." + FilenameUtils.getExtension(goodsimg.getOriginalFilename()) ));
		TGoods goods=new TGoods();
		goods.setGoodsName(request.getParameter("goodsname"));
		goods.setGoodsImg(request.getParameter("imgPath"));
		goods.setPrice(Double.parseDouble(request.getParameter("price")));
		if(goods.getPrice()<0) {
			JOptionPane.showMessageDialog(null, "ADD OK");
		}else {
			goods.setGoodsTitle("title");
			goods.setGoodsCateId(request.getParameter("cateid"));
//			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//�������ڸ�ʽ
//	        String createtime = df.format(new Date());// new Date()Ϊ��ȡ��ǰϵͳʱ��
	        Date date=new Date();
	        goods.setCreateTime(date);
	        goods.setUpdateTime(date);
			String uuid=UUID.randomUUID().toString();
			goods.setGoodsId(uuid);
			goods.setGoodsImg("");
			goods.setBarCode(request.getParameter("barcode"));
			ms.addGoods(goods);
			JOptionPane.showMessageDialog(null, "ADD OK");
		}
		return "admin/goodslist";
	}
	
	@RequestMapping("/reGoods")
	public String reGoods(HttpServletRequest request,HttpServletResponse response)throws Exception{
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
		TGoods goods=new TGoods();
		goods.setGoodsName(request.getParameter("goodsname"));
//		String name = request.getParameter("goodsname");
//		System.out.println("前台传入参数：" + name);
//		String gname  = URLDecoder.decode(name,"UTF-8");
//        System.out.println("经过解码后参数：" + gname);
        
		goods.setPrice(Double.parseDouble(request.getParameter("price")));
		
//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//�������ڸ�ʽ
//        String createtime = df.format(new Date());// new Date()Ϊ��ȡ��ǰϵͳʱ��
        Date date=new Date();
        goods.setUpdateTime(date);
		goods.setGoodsId(request.getParameter("goodsId"));
		System.out.println("将要更改的书目为："+goods);
		ms.reGoods(goods);
		JOptionPane.showMessageDialog(null, "MODIFY OK");
		return "admin/ok";
	}
	
	@RequestMapping("/reStock")
	public String reStock(HttpServletRequest request,HttpServletResponse response)throws Exception{
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
		String goodsId=request.getParameter("goodsId");
		int goodsStock=Integer.parseInt(request.getParameter("goodsStock"));
		TGoods goods=new TGoods();
		goods.setGoodsId(goodsId);
		goods.setGoodsStock(goodsStock);
		ms.reStock(goods);
		JOptionPane.showMessageDialog(null, "MODIFY OK");
		return "admin/ok";
	}
	
	@RequestMapping("/DimSearch")
	public String DimSearch(String goods_dimSearch,HttpSession session) {
		List<TGoods> list=cs.dimSelectService(goods_dimSearch);
		session.setAttribute("mdimgoods", list);
		return"admin/dimgoods";
	}
	
	@RequestMapping("/deleteGoods")
	public String deleteGoods(HttpServletRequest request)throws Exception{
		String goodsid=request.getParameter("goodsid");
		ms.deleteGoods(goodsid);
		return "admin/goodslist";
	}
	
}
